package cn.iocoder.yudao.module.wms.controller.admin.doc;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wms.controller.admin.doc.vo.*;
import cn.iocoder.yudao.module.wms.dal.dataobject.doc.DocDO;
import cn.iocoder.yudao.module.wms.service.doc.DocService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 仓储单据")
@RestController
@RequestMapping("/wms/doc")
@Validated
public class DocController {

    @Resource
    private DocService docService;

    @PostMapping("/create")
    @Operation(summary = "创建仓储单据")
    @PreAuthorize("@ss.hasPermission('wms:doc:create')")
    public CommonResult<Long> createDoc(@Valid @RequestBody DocSaveReqVO createReqVO) {
        return success(docService.createDoc(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新仓储单据")
    @PreAuthorize("@ss.hasPermission('wms:doc:update')")
    public CommonResult<Boolean> updateDoc(@Valid @RequestBody DocSaveReqVO updateReqVO) {
        docService.updateDoc(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除仓储单据")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:doc:delete')")
    public CommonResult<Boolean> deleteDoc(@RequestParam("id") Long id) {
        docService.deleteDoc(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得仓储单据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:doc:query')")
    public CommonResult<DocRespVO> getDoc(@RequestParam("id") Long id) {
        DocDO doc = docService.getDoc(id);
        return success(BeanUtils.toBean(doc, DocRespVO.class));
    }

    @GetMapping("/get/no")
    @Operation(summary = "获取单据编号")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:doc:create')")
    public CommonResult<String> getDocNo(@RequestParam("id") Long id) {
        String docNo = docService.getDocNo(id);
        return success(docNo);
    }

    @GetMapping("/page")
    @Operation(summary = "获得仓储单据分页")
    @PreAuthorize("@ss.hasPermission('wms:doc:query')")
    public CommonResult<PageResult<DocRespVO>> getDocPage(@Valid DocPageReqVO pageReqVO) {
        PageResult<DocDO> pageResult = docService.getDocPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DocRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出仓储单据 Excel")
    @PreAuthorize("@ss.hasPermission('wms:doc:export')")
    @OperateLog(type = EXPORT)
    public void exportDocExcel(@Valid DocPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DocDO> list = docService.getDocPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "仓储单据.xls", "数据", DocRespVO.class,
                        BeanUtils.toBean(list, DocRespVO.class));
    }

}